package web;

import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.OutputStream;
import java.util.List;

import javax.imageio.ImageIO;
import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import dao.AdminDao;
import dao.CostDao;
import dao.SaveDao;
import entity.Admin;
import entity.Cost;
import util.ImageUtil;

public class MainServlet extends HttpServlet {

	@Override
	protected void service(
			HttpServletRequest req, 
			HttpServletResponse res) throws ServletException, IOException {
		
		//获取路径
		String path = req.getServletPath();
		//根据规范处理路径
		if("/findCost.do".equals(path)){
			findCost(req,res);
		}else if("/addCost.do".equals(path)){
			addCost(req,res);
		}else if("/toAddCost.do".equals(path)){
			toAddCost(req,res);
		}else if("/toUpdateCost.do".equals(path)){
			toUpdateCost(req,res);	
		}else if("/toLogin.do".equals(path)){
			toLogin(req,res);
		}else if("/toIndex.do".equals(path)){
			toIndex(req,res);
		}else if("/login.do".equals(path)){
			login(req,res);
		}else if("/createImg.do".equals(path)){
			createImg(req,res);
		}else{
			throw new RuntimeException("没有这个页面");
		}
	}
	
	
	
	//生成验证码
	protected void createImg(
			HttpServletRequest req, 
			HttpServletResponse res) throws ServletException, IOException {
		
		//生成验证码及图片
		Object[] objs = ImageUtil.createImage();
		//将验证码存入session
		HttpSession session = req.getSession();
		session.setAttribute("imgCode",objs[0]);
		//将图片发送给浏览器
		res.setContentType("image/png");
		//获取字节输出流,该流由服务器创建,
		//其目标就是当前访问的那个浏览器
		OutputStream os = res.getOutputStream();
		BufferedImage im = (BufferedImage)objs[1];
		ImageIO.write(im, "png", os);
		os.close();
		
	}
	
	
	
	//查询资费
	protected void findCost(
			HttpServletRequest req, 
			HttpServletResponse res) throws ServletException, IOException {
		
		//查询所有资费;
		CostDao dao = new CostDao();
		List<Cost> list = dao.findAll();
		//将请求转发到jsp
		req.setAttribute("costs", list);
		//当前:/netctoss/findCost.do
		//目标:/netctoss/WEB-INF/cost/find.jsp
		
		req.getRequestDispatcher
			("WEB-INF/cost/find.jsp").forward(req, res);
		
	}
	
	//增加资费数据
	protected void addCost(
			HttpServletRequest req, 
			HttpServletResponse res) throws ServletException, IOException {
		
		req.setCharacterEncoding("utf-8");
		
		//获取参数;并存入cost对象中
		String name = req.getParameter("cost_name");
		String costType = req.getParameter("radFeeType");	
		
		
		String bd = req.getParameter("base_Duration");
		Integer baseDuration =0;
		if(bd!=null&&bd.length()>0){
			baseDuration = Integer.parseInt(bd);
		}
		
		String bc = req.getParameter("base_Cost");
		Double baseCost =0.0;
		if(bc!=null&&bc.length()>0){
			baseCost = Double.parseDouble(bc);
		}
			
		
		String uc = req.getParameter("unit_Cost");
		Double unitCost = 0.0;
		if(uc!=null&&uc.length()>0){
			unitCost =Double.parseDouble(uc);
		}
			
		
		String descr = req.getParameter("descr");
		
		Cost c = new Cost();
		c.setName(name);
		c.setBaseDuration(baseDuration);
		c.setBaseCost(baseCost);
		c.setUnitCost(unitCost);
		c.setDescr(descr);
		c.setCostType(costType);
		
		//保存cost对象到数据库
		SaveDao dao = new SaveDao();
		dao.save(c);
		
		//重定向到查询
		//当前:/netctoss/addCost.do
		//目标:/netctoss/findCost.do
		
		res.sendRedirect("findCost.do");
		
	}
	
	//使页面跳转到增加资费界面
	protected void toAddCost(
			HttpServletRequest req, 
			HttpServletResponse res) throws ServletException, IOException {
		
		
		//当前:/netctoss/toAddCost.do
		//目标:/netctoss/WEB-INF/cost/add.jsp
		
		req.getRequestDispatcher
			("WEB-INF/cost/add.jsp").forward(req, res);
		
	}

	//修改资费信息
	protected void toUpdateCost(
			HttpServletRequest req, 
			HttpServletResponse res) throws ServletException, IOException {
		
		Integer id = Integer.valueOf(req.getParameter("id"));
		CostDao dao = new CostDao();
		Cost cost = dao.findById(id);
		req.setAttribute("upCost", cost);
		req.getRequestDispatcher("WEB-INF/cost/update.jsp").forward(req, res);
		
	}

	//打开登录页面
	protected void toLogin(
			HttpServletRequest req, 
			HttpServletResponse res) throws ServletException, IOException {
		
		req.getRequestDispatcher("WEB-INF/main/login.jsp").forward(req, res);
		
	}
		
	
	protected void toIndex(
			HttpServletRequest req, 
			HttpServletResponse res) throws ServletException, IOException {
		
		req.getRequestDispatcher("WEB-INF/main/index.jsp").forward(req, res);
		
	}
	
	//登录
	protected void login(
			HttpServletRequest req, 
			HttpServletResponse res) throws ServletException, IOException {
		//接收参数
		String adminCode = req.getParameter("adminCode");
		String password = req.getParameter("password");
		String yzm_code = req.getParameter("yzm_code");

		//获取图片验证码的值
		HttpSession session2 = req.getSession();
		String imgCode = (String)session2.getAttribute("imgCode");
		//检查验证码是否正确
		if(yzm_code==null||!yzm_code.equalsIgnoreCase(imgCode)){
			//验证码错误
			req.setAttribute("error", "验证码错误");
			req.getRequestDispatcher("WEB-INF/main/login.jsp").forward(req, res);
			return;
		}
		
		
		//验证
		AdminDao dao = new AdminDao();
		Admin a = dao.findByCode(adminCode);
		
		if(a==null){
			//账号错误
			req.setAttribute("error","账号错误");
			req.getRequestDispatcher("WEB-INF/main/login.jsp").forward(req, res);;

		}else if(!a.getPassword().equals(password)){
			//密码错误
			req.setAttribute("error","密码错误");
			req.getRequestDispatcher("WEB-INF/main/login.jsp").forward(req, res);;
			
		}else{
			//将账号存入cookie
			Cookie c = new Cookie("user",adminCode);
			res.addCookie(c);
			
			//将账号存入session
			HttpSession session = req.getSession();
			session.setAttribute("user", adminCode);
			
			//验证通过
			res.sendRedirect("toIndex.do");
		}
		
		
		
	}
}